package com.eu.ebpm.engine.entity.deploy;

import java.util.List;

import com.eu.ebpm.Constants;
import com.eu.ebpm.engine.entity.BaseEntity;
import com.eu.ebpm.engine.entity.args.Param;
import com.eu.ebpm.engine.pvm.EngineServiceType;
import org.springframework.util.StringUtils;

/**
 * 
 * 任务部署定义
 * 
 * @author 王晓华
 *
 */
public class Mission extends BaseEntity {

	private Long deployId;

	private String name;

	private String desc;

	private String user;

	private String roler;

	private String org;

	private String assignee;

	private EngineServiceType type;

	private String serviceTaskExecute;

	private String filters;
	
	private String listener;

	// 关联部署信息
	private List<Transfer> transfers;
	private List<Transfer> toTransfers;
	private Deploy deploy;

	// 子流程部署缓冲字段
	private String subKey;
	private String subDesc;

	public Mission() {
	}

	public Mission(long deployId, String name, String desc, String user, String roler, String org, String assignee, String filters, String serviceTaskExecute, EngineServiceType type) {
		this.initId();
		this.deployId = deployId;
		this.name = name;
		this.desc = desc;
		this.user = user;
		this.roler = roler;
		this.org = org;
		this.assignee = assignee;
		this.type = type;
		this.filters = filters;
		this.serviceTaskExecute = serviceTaskExecute;
	}

	public String getSubKey() {
		return subKey;
	}

	public void setSubKey(String subKey) {
		this.subKey = subKey;
	}

	public String getSubDesc() {
		return subDesc;
	}

	public void setSubDesc(String subDesc) {
		this.subDesc = subDesc;
	}

	public long getDeployId() {
		return deployId;
	}

	public void setDeployId(long deployId) {
		this.deployId = deployId;
	}

	public List<Transfer> getTransfers() {
		return transfers;
	}

	public void setTransfers(List<Transfer> transfers) {
		this.transfers = transfers;
	}

	public EngineServiceType getType() {
		return type;
	}

	public void setType(EngineServiceType type) {
		this.type = type;
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public Deploy getDeploy() {
		return deploy;
	}

	public void setDeploy(Deploy deploy) {
		this.deploy = deploy;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDesc() {
		return desc;
	}

	public void setDesc(String desc) {
		this.desc = desc;
	}

	public String getUser() {
		return user;
	}

	public void setUser(String user) {
		this.user = user;
	}

	public String getRoler() {
		return roler;
	}

	public void setRoler(String roler) {
		this.roler = roler;
	}

	public String getOrg() {
		return org;
	}

	public void setOrg(String org) {
		this.org = org;
	}

	public String getAssignee() {
		return assignee;
	}

	public void setAssignee(String assignee) {
		this.assignee = assignee;
	}

	public String getFilters() {
		return filters;
	}

	public void setFilters(String filters) {
		this.filters = filters;
	}

	public String getListener() {
		return listener;
	}

	public void setListener(String listener) {
		this.listener = listener;
	}

	public void setDeployId(Long deployId) {
		this.deployId = deployId;
	}

	public String getServiceTaskExecute() {
		return serviceTaskExecute;
	}

	public void setServiceTaskExecute(String serviceTaskExecute) {
		this.serviceTaskExecute = serviceTaskExecute;
	}

	public List<Transfer> getToTransfers() {
		return toTransfers;
	}

	public void setToTransfers(List<Transfer> toTransfers) {
		this.toTransfers = toTransfers;
	}

	public void addToTransfer(Transfer transfer) {
		this.toTransfers.add(transfer);
	}

	public boolean existsCandidate() {
		return (getUser() == null && getOrg() == null && getRoler() == null);
	}
	
	public boolean isStartEventTargetRef() {
		for(Transfer transfer : getToTransfers()) {
			if(transfer.getFromMission().getType() == EngineServiceType.START_EVENT) {
				return true;
			}
		}
		return false;
	}
	
	public boolean isPermitToStart(Param arg) {
		if (!StringUtils.isEmpty(this.user)) {
			if (arg == null)
				return false;
			String[] users = this.user.split(",");
			for (String u : users) {
				if (u.equals(arg.get(Constants.STARTUP_USER)))
					return true;
			}
		}
		if (!StringUtils.isEmpty(this.org)) {
			if (arg == null)
				return false;
			String[] users = this.org.split(",");
			for (String u : users) {
				if (u.equals(arg.get(Constants.STARTUP_ORG)))
					return true;
			}
		}
		if (!StringUtils.isEmpty(this.roler)) {
			if (arg == null)
				return false;
			String[] users = this.roler.split(",");
			for (String u : users) {
				if (u.equals(arg.get(Constants.STARTUP_ROLER)))
					return true;
			}
		}
		return true;
	}

}
